package org.windowkit.toolkit.widget;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.SwingConstants;

import org.windowkit.toolkit.icons.ArrowIcon;
import org.windowkit.toolkit.util.LayoutUtils;


/**
 * This class provides a component which is to be used to simulate closing a
 * details window, selecting the next or previous row in the list and then
 * opening the details window again. To page through records in any other
 * fashion use PageSelector.
 *
 * @author Eric Lundin
 */
@SuppressWarnings("serial")
public class RecordSelector extends JComponent implements ActionListener {

	/** Constant to show previous record requested */
	public static final String PREVIOUS = "PREVIOUS";

	/** Constant to show next record requested */
	public static final String NEXT = "NEXT";

	/** << Previous button */
	protected JButton previous;

	/** >> Next button */
	protected JButton next;

	/** List of listeners to forward events to */
	ArrayList<ActionListener> listenerList = new ArrayList<ActionListener>();

	public RecordSelector() {
		initComponents();
		layoutComponents();
	}

	/**
	 * Initialize our components.
	 */
	public void initComponents() {
        previous = new JButton(new ArrowIcon(SwingConstants.WEST));
        next = new JButton(new ArrowIcon(SwingConstants.EAST));
		previous.setToolTipText("<html>Previous Record</html>");
		next.setToolTipText("<html>Next Record</html>");
		previous.setActionCommand(PREVIOUS);
		next.setActionCommand(NEXT);

		next.addActionListener(this);
		previous.addActionListener(this);
	}

	/**
	 * Layout our components.
	 */
	public void layoutComponents() {
		setLayout(new GridBagLayout());
		GridBagConstraints gbc = LayoutUtils.createConstraintsFillY();
		add(previous, gbc);
		add(next, gbc);

	}

	/**
	 * Add an action listener.
	 *
	 * @param listener
	 *            The listener to add.
	 */
	public void addActionListener(final ActionListener listener) {
		if (listener != null) {
			listenerList.add(listener);
		}
	}

	/**
	 * Remove an action listener.
	 *
	 * @param listener
	 *            The listener to remove.
	 */
	public void removeActionListener(final ActionListener listener) {
		listenerList.remove(listener);
	}

	/**
	 * {@inheritDoc}
	 */
	public void actionPerformed(final ActionEvent e) {
		ActionEvent event = new ActionEvent(this, e.getID(), e
				.getActionCommand());
		for (ActionListener current : listenerList) {
			current.actionPerformed(event);
		}
	}

    /**
     * Set the next button enabled/disabled.
     *
     * @param bool
     */
    public void setNextEnabled(final boolean bool){
        next.setEnabled(bool);
    }

    /**
     * Set the previous button enabled/disabled.
     *
     * @param bool
     */
    public void setPreviousEnabled(final boolean bool){
        previous.setEnabled(bool);
    }
}
